package org.csu.corp.controller;

import net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression;
import org.apache.commons.collections4.map.HashedMap;
import org.csu.corp.entity.Employee;
import org.csu.corp.entity.News;
import org.csu.corp.entity.Sign;
import org.csu.corp.persistence.EmployeeMapper;
import org.csu.corp.persistence.NewsMapper;
import org.csu.corp.persistence.SignMapper;
import org.csu.corp.util.SortByLetterAscUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/test")
public class testController {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SignMapper signMapper;

    @Autowired
    private NewsMapper newsMapper;

    @RequestMapping("/getWords")
    public Map<String,Object> getWords() {
        System.out.println("test");
        Map<String, Object> map = new HashedMap<String, Object>();
        String message = "test words";
        map.put("message", message);
        return map;
    }
    @PostMapping("/address")
    public Map<String,Object> getMsg(@RequestBody HashMap address){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String msg="";
        int flag = 0;
        Date date = new Date();
        String td = sdf.format(date);
        Employee employee = employeeMapper.getEmployeeById(new Integer((Integer) address.get("id")));
        List<Sign> list = signMapper.getSignByWorkIdAndClockInTime(employee.getWorkID());
        for(int i=0;i<list.size();i++){
            String ud = sdf.format(list.get(i).getClockInTime());
            System.out.println(ud+td);
            if(ud.equals(td)) {
                if (list.get(i).getClockOutTime() == null) {
                    list.get(i).setClockOutAddr(address.get("name").toString());
                    System.out.println(address.get("name").toString());
                    list.get(i).setClockOutTime(date);
                    signMapper.updateByPrimaryKey(list.get(i));
                    msg = "下班打卡成功，辛苦了！";
                    flag = 1;
                    break;
                } else {
                    msg = "您今天已完成两次打卡";
                    flag = 1;
                    break;
                }
            }
        }
        if(flag==0){
            Sign sign = new Sign();
            sign.setWorkId(employee.getWorkID());
            sign.setClockInAddr(address.get("name").toString());
            System.out.print(address.get("name").toString());
            sign.setClockInTime(date);
            signMapper.insert(sign);
            msg = "上班打卡成功，开启元气满满的一天吧！";
        }
        System.out.print(date);

        java.sql.Date da = new java.sql.Date(1);
        System.out.print(da);
        Map<String,Object> map = new HashedMap<>();
        map.put("msg",msg);
        map.put("date",date);
        return map;
    }
    @PostMapping("/employee")
    public Map<String,Object> getEmp(@RequestBody HashMap id){
        Employee employee = new Employee();
        employee = employeeMapper.getEmployeeById(Integer.valueOf((String)id.get("id")));

        Map<String,Object> map = new HashedMap<>();
        map.put("employee",employee);
        return map;
    }

    @PostMapping("/colleague")
    public Map<String, Object> getColleague(@RequestBody HashMap id) throws Exception {
        System.out.print(123);
        Employee employee = employeeMapper.getEmployeeById(new Integer((Integer) id.get("id")));
        List<Employee> list = employeeMapper.getEmployeeByDept(employee.getDepartmentId());
        Map<String,Object> map = new HashedMap<>();
        map = SortByLetterAscUtils.sortByLetterAsc(list, Employee.class, "name");
        System.out.print(map.get("data"));
        return map;
    }

    @GetMapping("/news")
    public Map<String,Object> getNews(){
        List<News> newsList = newsMapper.getList();
        List<News> news = new ArrayList<>();
        List<News> inform = new ArrayList<>();
        for(int i=0;i<newsList.size();i++){
            if(newsList.get(i).getKind()==1)
                news.add(newsList.get(i));
            else
                inform.add(newsList.get(i));
            System.out.println(newsList.get(i));
        }
        Map<String,Object> map = new HashedMap<>();

        map.put("news",news);
        map.put("inform",inform);
        return map;
    }

    @PostMapping("/sign")
    public Map<String,Object> getSign(@RequestBody HashMap id){
        Employee employee = employeeMapper.getEmployeeById(new Integer((Integer) id.get("id")));
        List<Sign> signList = signMapper.getSignByWorkIdAndClockInTime(employee.getWorkID());
        Map<String, Object> map = new HashedMap<>();
//        System.out.print(signList);
        map.put("sign",signList);
        return map;
    }
}
